package com.dengdai.applibrary.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dengdai.applibrary.R;
import com.dengdai.applibrary.network.NetWork;
import com.dengdai.applibrary.view.custom.LoadDataEemptyView;
import com.dengdai.pullrefresh.library.PullToRefreshBase;
import com.dengdai.pullrefresh.library.PullToRefreshWebView;

/**
 * @说明 加载网页基类
 * @作者 LY
 * @时间 2015/12/30 13:46
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public abstract class BaseWebViewFragment extends BaseFragment implements IBaseWebView {
    public PullToRefreshWebView web_view;
    public LoadDataEemptyView load_data_view;
    private Handler myHandler;

    @Override
    public int getLayoutResID() {
        return R.layout.common_base_webview;
    }

    @Override
    public void initView(View view) {
        web_view = (PullToRefreshWebView) view.findViewById(R.id.web_view);
        load_data_view = (LoadDataEemptyView) view.findViewById(R.id.load_data_view);
    }

    @Override
    public void business(Context context) {
        web_view.setMode(getMode());
        WebSettings webSettings = web_view.getRefreshableView().getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setGeolocationEnabled(true);
        webSettings.setDomStorageEnabled(true);
        GeoClient geo = new GeoClient();
        web_view.getRefreshableView().setWebViewClient(new PTCWebViewClient(getActivity(), web_view.getRefreshableView()));
        web_view.getRefreshableView().setWebChromeClient(geo);
        String origin = "";
        geo.onGeolocationPermissionsShowPrompt(origin, new GeolocationPermissions.Callback() {
            @Override
            public void invoke(String origin, boolean allow, boolean retain) {

            }
        });
        web_view.getRefreshableView().setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    load_data_view.setVisibility(View.GONE);
                    web_view.onRefreshComplete();
                } else {
                    load_data_view.setVisibility(View.VISIBLE);
                }
            }
        });
        web_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<WebView>() {
            @Override
            public void onRefresh(PullToRefreshBase<WebView> refreshView) {
                web_view.getRefreshableView().loadUrl(getLoadUrl());
            }
        });
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        if (NetWork.getNetworkStatus(BaseApplication.getAppContext())) {
            if (!TextUtils.isEmpty(getLoadUrl())) {
                web_view.getRefreshableView().loadUrl(getLoadUrl());
                load_data_view.loadData();
                load_data_view.setVisibility(View.VISIBLE);
            } else {
                load_data_view.loadDataError();
                load_data_view.setVisibility(View.VISIBLE);
            }
        } else {
            load_data_view.setVisibility(View.VISIBLE);
            load_data_view.netWorkError();
        }
    }

    public class GeoClient extends WebChromeClient {
        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            super.onGeolocationPermissionsShowPrompt(origin, callback);
            callback.invoke(origin, true, false);
        }
    }

    public class PTCWebViewClient extends WebViewClient {
        public PTCWebViewClient(Context con, final WebView web) {
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            load_data_view.setVisibility(View.VISIBLE);
            load_data_view.loadDataError();
        }
    }

}
